'use strict';

stareal
    .controller("BeautifulImgDetailController", function ($scope, $api, $stateParams, $state, $lazyLoader,$timeout,localStorageService,$alert) {
        $scope.mypage = 2;
        $scope.listpage= 1;
        $scope.id=$stateParams.id;
        if($stateParams.app==1){
            $scope.BackStatus=true;
            $scope.dowload_appStatus=true;
        }
        $scope.closedDowload=function(){
            $scope.dowload_appStatus=false;
        }
        $scope.sharUrl='https://test.djx.m.opentickets.cn/?&#/'; // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致'
        // 加载对应演出种类的内容  app/beautychart/list  id
        //分享
        //微信分享http://192.168.1.4:9090/oauth/getSignature
        $scope.getShare=function(){
            $api.get("app/share/getSignature", {url: window.location.href.split('#')[0]})
                .then(function (ret) {

                    if (ret) {
                        var data = ret.data;
                        wx.config({
                            debug: false,
                            appId: data.appid,
                            timestamp: data.timestamp,
                            nonceStr: data.nonceStr,
                            signature: data.signature,
                            jsApiList: [
                                'onMenuShareTimeline',
                                'onMenuShareAppMessage',
                                'onMenuShareQQ',
                                // 'onMenuShareWeibo',
                                'onMenuShareQZone'
                            ]
                        });
                        wx.ready(function () {
                            //分享到朋友圈
                            wx.onMenuShareTimeline({
                                title: $scope.BeautifulImgdetail.name, // 分享标题
                                desc: '我在独角秀分享了一套不错的美图！', // 分享描述
                                link: $scope.sharUrl+'main/beautifulImgDetail/'+$stateParams.id+'/1', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                                imgUrl: $scope.BeautifulImgdetail.list[0].img_url, // 分享图标
                                success: function () {
                                    // 用户确认分享后执行的回调函数
                                    //alert('你好');
                                },
                                cancel: function () {
                                    // 用户取消分享后执行的回调函数
                                    // alert('你好....');
                                }
                            });
                            //分享给朋友
                            wx.onMenuShareAppMessage({
                                title: $scope.BeautifulImgdetail.name, // 分享标题
                                desc: '我在独角秀分享了一套不错的美图！', // 分享描述
                                link: $scope.sharUrl+'main/beautifulImgDetail/'+$stateParams.id+'/1', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                                imgUrl: $scope.BeautifulImgdetail.list[0].img_url, // 分享图标
                                type: '', // 分享类型,music、video或link，不填默认为link
                                dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
                                success: function () {
// 用户确认分享后执行的回调函数
                                    // alert('你好....');
                                },
                                cancel: function () {
// 用户取消分享后执行的回调函数
                                }
                            });
                            wx.onMenuShareQQ({
                                title: $scope.BeautifulImgdetail.name, // 分享标题
                                desc: '我在独角秀分享了一套不错的美图！', // 分享描述
                                link: $scope.sharUrl+'main/beautifulImgDetail/'+$stateParams.id+'/1', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                                imgUrl: $scope.BeautifulImgdetail.list[0].img_url, // 分享图标
                                success: function () {
// 用户确认分享后执行的回调函数
                                },
                                cancel: function () {
// 用户取消分享后执行的回调函数
                                }
                            });
                            wx.onMenuShareQZone({
                                title: $scope.BeautifulImgdetail.name, // 分享标题
                                desc: '我在独角秀分享了一套不错的美图！', // 分享描述
                                link: $scope.sharUrl+'main/beautifulImgDetail/'+$stateParams.id+'/1', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                                imgUrl: $scope.BeautifulImgdetail.list[0].img_url, // 分享图标
                                success: function () {
// 用户确认分享后执行的回调函数
                                },
                                cancel: function () {
// 用户取消分享后执行的回调函数
                                }
                            });
                        });
                        wx.error(function (res) {
                            //console.log(res);
                            //alert("微信分享接口配置失败");
                        });
                    }
                })
        }

        $api.get("app/beautychart/findById", {id: $stateParams.id}, true)
            .then(function (ret) {
                var data=ret.data;
                $scope.BeautifulImgdetail=data;
                $scope.getShare();
            })
        $scope.completebeatufulstRepeat=function(){
            var height=(document.body.clientHeight-parseInt(document.documentElement.style.fontSize)*3.0)+'px';
            // console.log(height);
            $(".img-gather-slide").height(height);
            var swiper = new Swiper('.img-gather', {
                pagination: '.img-gather-pagination',//分页容器
                observer:true,//修改swiper自己或子元素时，自动初始化swiper
            });

        }
        $scope.ImgCollect=function(){
            var  rs = "main.beautifulImgDetail-" + JSON.stringify({id: $stateParams.id});
            var token=localStorageService.get('token');
            var isbind = localStorageService.get('isbind');
            var openID = localStorageService.get('openid');
            if(isbind!=1&&openID!=''){
                localStorageService.set('cleartoken',token);
                location.href = "#/main/register/"+encodeURIComponent(rs);
                localStorageService.set('token','');
                return false
            }
            if (!localStorageService.get('token')) {
                var  rs = "main.beautifulImgDetail-" + JSON.stringify({id: $stateParams.id});
                var ua = window.navigator.userAgent.toLowerCase();
                if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                    // 正式地址
                    location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                        "appid=wxda73ac8ac7af1261&" +
                        "redirect_uri=http%3A%2F%2Ftest.djx.m.opentickets.cn%2Foauth%2Findex" +
                        "&response_type=code&scope=snsapi_userinfo&state="+encodeURIComponent(rs);
                } else {
                    location.href = "#/main/login/"+encodeURIComponent(rs);
                }
                return false;
            }

            $api.post("app/view/collection", {viewid: $stateParams.id,type:8}, true)
                .then(function (ret) {
                    if (ret.follow == 1) {
                        $alert.show('收藏成功！')
                        $scope.BeautifulImgdetail.favor=1;
                    }else{
                        $alert.show('取消收藏！');
                        $scope.BeautifulImgdetail.favor=0;
                    }

                })

        }




    });